<?php
/**
 * @author thanhnv
 * @desc 26Jun2008
 * 
 * @desc convert csv=>database
 * @desc insert province & city from csv file to db
 */
set_time_limit(0);
include_once (PATH_SQL . 'category_sql.php');
$sPath = PATH_CONTROL_ADMIN;
$d = dir($sPath);

function check_name($name,$db_category)
{
	$sSql="SELECT * FROM ams_product_category WHERE product_category_name like '{$name}'";
	return $db_category->getRecordsetExtend($sSql);
}

function check_child($name,$category_id)
{
	$sSql="SELECT * FROM ams_product_category WHERE product_category_name like '{$name}'AND product_category_id='{$category_id}'";
	return $db_category->getRecordsetExtend($sSql);
}

	$db_category=new Db_Categoiry();
	$fileCSV = $sPath . "category.csv";
	
	$handle = fopen($fileCSV, "r");
	$sort = 0;
	$count=0;
	$total=0;
	while (($data = fgetcsv($handle,1000, ",")) !== FALSE && $sort<100); 
	{
		if($sort==0)
		{
			
		}
		else
		{
				//$arr_temp=mb_convert_encoding($arr_temp,"SJIS","UTF-8");
				$arr=explode('>',$data[1]);
				$count=count($arr);
				$index=$count-1;	
				for($i=1;$i<$count;$i++)
					{
						$arr[$i]=mb_convert_encoding($arr[$i],"UTF-8","SJIS");
						$sSql1=null;
						if($i==$count-1)
						{
							$sSql = "INSERT INTO ams_product_category (product_category_yahoo_id,product_category_name)
								 VALUES ('{$data[0]}','{$arr[$i]}')";
							 
							 $db_category->doUpdateExtend($sSql);
						}
						else
						{
							if(!check_name($arr[$i],$db_category))
							{
								$sSql = "INSERT INTO ams_product_category (product_category_yahoo_id,product_category_name,product_category_parent_id)
								 VALUES ('{$data[0]}','{$arr[$i]}',0)";
							 $total++;
							 $db_category->doUpdateExtend($sSql);
							}
						}
					}
		}
		$sort++;
		//echo $sSql;
	
	}
	fclose($handle);
//}
$d->close();


/**
 * @desc finished:
 * province.csv
 * 1.hokaido.csv
 * 2.aomori.csv
 * 3.iwate.csv
 * 4.miyagi.csv
 */
?>

?>